﻿using RealtorsPortal.Business_Logic_Layer;
using RealtorsPortal.Data_Access_Layer;
using RealtorsPortal.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RealtorsPortal
{
    public partial class Advertisements : System.Web.UI.Page
    {
        AdvertisementsDAL advDAL = new AdvertisementsDAL();
        AdsImagesDAL adsImagDAL = new AdsImagesDAL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack){
                LoadAllAdvertiseToGridview();
            }
        }



        private void LoadAllAdvertiseToGridview()
        {
            DataTable dt = new DataTable();
            AdvertisementsDAO advObj = new AdvertisementsDAO();
            //advObj.UserId = Convert.ToInt16(Session["adminId"]);
            dt = advDAL.getAdvertisementsData();
            grvManageAdver.DataSource = dt;
            grvManageAdver.DataBind();
        }

        protected void grvManageAdver_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "BrowseComponent")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                string AdvId = grvManageAdver.DataKeys[index].Value.ToString();
                //Response.Redirect("~/AdminCP/EditAdvertisement.aspx?ID="+AdvId);
                Server.Transfer("~/AdminCP/AdvertiseAdDetails.aspx?ID=" + AdvId);
            }
        }

        protected void btnDeleteSelected_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow gvrow in grvManageAdver.Rows)
            {
                CheckBox chkdelete = new CheckBox();
                //Finiding checkbox control in gridview for particular row
                chkdelete = (CheckBox)gvrow.FindControl("chkAdvertisements");

                //Condition to check checkbox selected or not
                if (chkdelete.Checked == true)
                {
                    //Getting UserId of particular row using datakey value

                    int id = Convert.ToInt32(grvManageAdver.DataKeys[gvrow.RowIndex].Value);
                    if (DeleteRowAtAdsImages(id) > 0)
                    {
                        DeleteRowAtAdvertisement(id);
                    }
                }
            }
            LoadAllAdvertiseToGridview();
        }

        private void DeleteRowAtAdvertisement(int id)
        {
            AdvertisementsDAO advObj = new AdvertisementsDAO();
            advObj.AdvId = id;
            advDAL.DeleteAdvertisements(advObj);
        }

        private int DeleteRowAtAdsImages(int id)
        {
            AdsImages adsImageObj = new AdsImages();
            adsImageObj.AdvId = id;
            return adsImagDAL.DeleteAdsImages(adsImageObj);
        }

        protected void grvManageAdver_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvManageAdver.PageIndex = e.NewPageIndex;
            this.LoadAllAdvertiseToGridview();
        }
    }
}